package com.woniuxy.userassembly.apiimpl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.api.RbacUserApi;
import com.woniuxy.common.PageResult;
import com.woniuxy.common.Result;
import com.woniuxy.form.RbacUserForm;
import com.woniuxy.userassembly.config.JWTHelper;
import com.woniuxy.userassembly.dao.RbacUserMapper;
import com.woniuxy.userassembly.model.RbacRole;
import com.woniuxy.userassembly.model.RbacUser;
import com.woniuxy.userassembly.service.IRbacUserRoleService;
import com.woniuxy.userassembly.service.IRbacUserService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import sun.rmi.runtime.Log;

import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Liyfi
 * @since 2021-05-22
 */
@RestController
@Slf4j
public class RbacUserController implements RbacUserApi {
    @Autowired
    private IRbacUserService service;

    @Autowired
    private IRbacUserRoleService userRoleService;

    @Autowired
    private JWTHelper jwtHelper;

    @Autowired
    private RbacUserMapper mapper;

    
    
    
    @Override
    public Result addUser(RbacUserForm form) {
        RbacUser user = new RbacUser();
        BeanUtils.copyProperties(form,user);
        service.register(user);
        return Result.success("成功");
    }

    /**
     *
     * 登录
     * @param form
     * @return
     */
    @Override
    public Result platformLogin(RbacUserForm form) {
        log.info("form,{}",form);
        RbacUser user = new RbacUser();
        BeanUtils.copyProperties(form,user);
        RbacUser user1 = service.login(user);
        if(user1!= null){
            HashMap<String, Object> payLoad = new HashMap<>();
            payLoad.put("rbacUserId",user.getUserId());
            String jwt = jwtHelper.createBossJWT(DateUtils.addDays(new Date(), 1), payLoad);
            return Result.success(jwt);
        }else {
            return Result.fail("账号或密码错误");
        }
    }

    @Transactional
    @Override
    public Result deleteUser(int userId) {
        int i = service.delUser(userId);
        if(i == 1){
            int i1 = userRoleService.delUserRole(userId);
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @Override
    public Result updUser(RbacUserForm form) {
        RbacUser oldUser = mapper.findUserById(form.getUsername());
        if(oldUser != null){
            RbacUser user = new RbacUser();
            int userId = oldUser.getUserId();
            BeanUtils.copyProperties(form,user);
            user.setUserId(userId);
            int i = service.updUser(user);
            if(i == 1){
                return Result.success("修改成功");
            }
        }

        return Result.fail("修改失败");
    }

    @Override
    public PageResult findUser(int pageIndex) {
        Page<RbacUser> pageParam = new Page<>(pageIndex,10L);
        IPage<RbacUser> pageResult = service.page(pageParam);
        PageResult<RbacUser> myPageResult = new PageResult<RbacUser>((int) pageResult.getTotal(), (int) pageResult.getCurrent(), (int) pageResult.getSize(), pageResult.getRecords());
        myPageResult.setSuccess(true);
        return myPageResult;
    }
}

